﻿Imports BUS
Imports DevExpress.XtraTreeList.Nodes
Imports DevExpress.XtraTreeList
Imports DTO
Imports System.Threading

Public Class frmThemVaiTro
    Public Property flag As Boolean
    Private dsChucNang As DataTable
    Dim dt As DataTable
    Private dsQuyenNhomND As List(Of PhanQuyenNhonNDDTO)
    Private maNhom As String
    Public Delegate Sub loadLaiUC()
    Public Event suKienTaiLaiUC As loadLaiUC
    Private capNhat As Boolean = False
    Private nhomND As NhomNDDTO
    Private thread As Thread
    Private pqNhomND As PhanQuyenNhonNDDTO


    Public Sub New(_maNhom As String, pq As PhanQuyenNhonNDDTO)
        InitializeComponent()
        Me.maNhom = _maNhom
        Me.pqNhomND = pq
    End Sub


    Private Sub btnDong_Click(sender As Object, e As EventArgs) Handles btnDong.Click
        Me.Close()
    End Sub

    Private Sub frmThemVaiTro_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        If maNhom <> "" Then
            'maNhom = VaiTroBUS.layMaVaiTroTiepTheo()
            dsChucNang = ChucNangBUS.layChucNang(maNhom)
            For Each row As DataRow In dsChucNang.Rows
                Dim node1 As TreeListNode = Nothing
                node1 = treePhanQuyen.FindNodeByID(Integer.Parse(row("MaCha").ToString) - 1)
                If node1 Is Nothing Then
                    treePhanQuyen.AppendNode(New Object() {row("TenChucNang").ToString, row("TatCa"), row("TruyCap"), row("Them"), row("Xoa"), row("Sua"), row("In"), row("Nhap"), row("Xuat")}, Integer.Parse(row("MaChucNang").ToString) - 1)
                Else
                    treePhanQuyen.AppendNode(New Object() {row("TenChucNang").ToString, row("TatCa"), row("TruyCap"), row("Them"), row("Xoa"), row("Sua"), row("In"), row("Nhap"), row("Xuat")}, node1)
                End If
            Next
            nhomND = NhomNDBUS.layDsNhomNDBangMa(maNhom)
            txtDienGiai.Text = nhomND.DienGiai
            txtTen.Text = nhomND.TenNhom
            ckbKichHoat.Checked = nhomND.KichHoat
            Me.Text = "Cập Nhật Nhóm Người Dùng " + nhomND.TenNhom
            capNhat = True
        Else
            maNhom = VaiTroBUS.layMaVaiTroTiepTheo()
            dsChucNang = ChucNangBUS.layChucNang("")
            For Each row As DataRow In dsChucNang.Rows
                Dim node1 As TreeListNode = Nothing
                node1 = treePhanQuyen.FindNodeByID(Integer.Parse(row("MaCha").ToString) - 1)
                If node1 Is Nothing Then
                    treePhanQuyen.AppendNode(New Object() {row("TenChucNang").ToString, True, True, True, True, True, True, True, True}, Integer.Parse(row("MaChucNang").ToString) - 1)
                Else
                    treePhanQuyen.AppendNode(New Object() {row("TenChucNang").ToString, True, True, True, True, True, True, True, True}, node1)
                End If
            Next
        End If
        txtMa.Text = maNhom

    End Sub

    Private Sub btnLuu_Click(sender As Object, e As EventArgs) Handles btnLuu.Click

        thread = New Thread(AddressOf Me.showManHinhCho)
        thread.IsBackground = True
        thread.Start()
        FlyoutPanel2.ShowPopup()
        'If  Then
        'End If

    End Sub

    Private Sub treePhanQuyen_CellValueChanging(sender As Object, e As CellValueChangedEventArgs) Handles treePhanQuyen.CellValueChanging
        Dim colIndex As Integer
        For i = 0 To treePhanQuyen.Columns.Count - 1
            If treePhanQuyen.Columns(i).Name = e.Column.Name Then
                colIndex = i
            End If
        Next
        If e.Column.Name = "colTaTCa" Then
            e.Node.SetValue(colIndex, e.Value)
            duyetNodeCotTatCa(e.Node, colIndex, e.Value)
        Else
            e.Node.SetValue(colIndex, e.Value)
            duyetnode(e.Node, colIndex, e.Value)
        End If

    End Sub

    Private Sub duyetnode(node As TreeListNode, colName As Object, valuse As Object)
        If node.HasChildren Then
            For Each nodeChild As TreeListNode In node.Nodes
                nodeChild.SetValue(colName, valuse)
                duyetnode(nodeChild, colName, valuse)
            Next
        End If
    End Sub

    Private Sub duyetNodeCotTatCa(node As TreeListNode, colIndex As Integer, valuse As Object)
        For i = colIndex To treePhanQuyen.Columns.Count - 1
            node.SetValue(i, valuse)
        Next
        If node.HasChildren Then
            For Each nodeChild As TreeListNode In node.Nodes
                For i = colIndex To treePhanQuyen.Columns.Count - 1
                    nodeChild.SetValue(i, valuse)
                    duyetnode(nodeChild, i, valuse)
                Next
            Next
        End If
    End Sub


    Private Sub txtTen_Leave(sender As Object, e As EventArgs) Handles txtTen.Leave
        If txtTen.Text.Length <= 0 Then
            lbLoi.Text = "Vui lòng nhập thông tin"
            Dim loca As New Point(80, 52)
            txtTen.Width = 242
            txtTen.Location = loca
        Else
            Dim loca As New Point(57, 52)
            txtTen.Width = 265
            txtTen.Location = loca
        End If
    End Sub

    Private Sub PictureEdit1_MouseHover(sender As Object, e As EventArgs) Handles PictureEdit1.MouseHover
        FlyoutPanel1.ShowBeakForm()
    End Sub

    Private Function checkDataInPut() As Boolean
        If txtTen.Text.Length <= 3 Then
            DevExpress.XtraEditors.XtraMessageBox.Show("Các ô nhậ liệu không được để trống", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Return False
        End If
        Return True
    End Function

    Private Sub showManHinhCho()
        If checkDataInPut() Then
            If capNhat = False Then
                If pqNhomND.Them = True Then
                    Dim nhom As New NhomNDDTO
                    nhom.TenNhom = txtTen.Text
                    nhom.DienGiai = txtDienGiai.Text
                    nhom.KichHoat = ckbKichHoat.Checked
                    Dim kq As String = NhomNDBUS.themNhom(nhom, treePhanQuyen, maNhom)
                    If kq <> "-1" Then
                        maNhom = VaiTroBUS.layMaVaiTroTiepTheo()
                        txtMa.Text = maNhom
                        RaiseEvent suKienTaiLaiUC()
                        DevExpress.XtraEditors.XtraMessageBox.Show("Thêm nhóm người dùng thành công", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information)
                        HeThongBUS.themNhatKy(frmMain.maND, "Hệ Thống", "Thêm Vai Trò", "Thêm Vai Trò " + maNhom + " - " + txtTen.Text)
                    End If
                Else
                    DevExpress.XtraEditors.XtraMessageBox.Show("Bạn không có quyền thêm", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning)
                End If
                
            Else
                If pqNhomND.Sua = True Then
                    Dim nhom As New NhomNDDTO
                    nhom.MaNhom = maNhom
                    nhom.TenNhom = txtTen.Text
                    nhom.DienGiai = txtDienGiai.Text
                    nhom.KichHoat = ckbKichHoat.Checked
                    Dim kq As Integer = NhomNDBUS.capNhatNhom(nhom, treePhanQuyen, maNhom)
                    If kq > 0 Then
                        RaiseEvent suKienTaiLaiUC()
                        DevExpress.XtraEditors.XtraMessageBox.Show("Cập nhật nhóm người dùng thành công", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information)
                        HeThongBUS.themNhatKy(frmMain.maND, "Hệ Thống", "Cập Nhật Vai Trò", "Cập Nhật Vai Trò " + maNhom + " - " + txtTen.Text)
                    End If
                Else
                    DevExpress.XtraEditors.XtraMessageBox.Show("Bạn không có quyền sữa", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning)
                End If
                
            End If
        End If
        tatManHinhCho()
        thread.Abort()
    End Sub

    Private Delegate Sub tatMHinhCho()
    'Private Function dongManHinhCho() As Object
    '    Dim o As New Object
    '    FlyoutPanel2.HidePopup()
    '    Return o
    'End Function

    Private Sub tatManHinhCho()
        If Me.FlyoutPanel2.InvokeRequired() Then
            Me.Invoke(New tatMHinhCho(AddressOf tatManHinhCho))
        Else
            FlyoutPanel2.HidePopup()
        End If
    End Sub


End Class